﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CarsManagement.Models;

namespace CarsManagement.Controllers
{
    public class CarsinfosController : Controller
    {
        
        private DB db = new DB();

        // GET: Carsinfos
        public ActionResult Index()
        {
            //获取搜索参数 e.GetType==type
            string Address = Request.Form["Address"];
            var carInfo = db.infos.Where(e => (e.Addresss.Contains(Address)|| Address == "" || Address == null));
            return View(carInfo.ToList());
        }

        // GET: Carsinfos/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            infos infos = db.infos.Find(id);
            if (infos == null)
            {
                return HttpNotFound();
            }
            return View(infos);
        }

        // GET: Carsinfos/Create
        public ActionResult Create()
        {
            ViewBag.TypeId = new SelectList(db.typee, "Id", "CarType");
            return View();
        }

        // POST: Carsinfos/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Phonenum,Number,licence,Addresss,States,TypeId")] infos infos)
        {
            if (ModelState.IsValid)
            {
                db.infos.Add(infos);
                db.SaveChanges();
                return Content("<script>alert('添加成功');location.href = '/Carsinfos/Index/';</script>");
            }

            ViewBag.TypeId = new SelectList(db.typee, "Id", "CarType", infos.TypeId);
            return View(infos);
        }

        public ActionResult Edits(int id)
        {
            infos infos = db.infos.Find(id);
            if (infos.States == "在库")
            {
                infos.States = "出库";
            }
            else
            {
                infos.States = "在库";
            }
            db.Entry(infos).State = EntityState.Modified;
            db.SaveChanges();
            return Content("<script>alert('状态更改成功');location.href = '/Carsinfos/Index/';</script>");
        }
        // GET: Carsinfos/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            infos infos = db.infos.Find(id);
            if (infos == null)
            {
                return HttpNotFound();
            }
            ViewBag.TypeId = new SelectList(db.typee, "Id", "CarType", infos.TypeId);
            return View(infos);
        }
        [HttpPost]
        public JsonResult DeleteBatch(string car)
        {
            car = car.Substring(0, car.Length - 1); //删除字符串最后一个字符
            string[] datalist = car.Split(',');
            foreach (var item in datalist)
            {
                int s = int.Parse(item);
                var b = db.infos.FirstOrDefault(c => c.Id == s);
                db.infos.Remove(b);
            }
            db.SaveChanges();
            return Json(new { state = "1000" });
        }

        [HttpPost]
        public ActionResult Del(int id)
        {
            infos infos = db.infos.Find(id);
            if (infos == null)
            {
                return Json(new { state = -1, msg = "删除失败，该车辆不存在！" });
            }
            db.infos.Remove(infos);
            db.SaveChanges();
            return Json(new { state = 0, msg = "删除成功！" });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Phonenum,Number,licence,Addresss,States,TypeId")] infos infos)
        {
            if (ModelState.IsValid)
            {
                db.Entry(infos).State = EntityState.Modified;
                db.SaveChanges();
                return Content("<script>alert('编辑成功');location.href = '/Carsinfos/Index/';</script>");
            }
            ViewBag.TypeId = new SelectList(db.typee, "Id", "CarType", infos.TypeId);
            return View(infos);
        }

        // GET: Carsinfos/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            infos infos = db.infos.Find(id);
            if (infos == null)
            {
                return HttpNotFound();
            }
            return View(infos);
        }

        // POST: Carsinfos/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            infos infos = db.infos.Find(id);
            db.infos.Remove(infos);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
       


        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
